

#include <cegfx/ce3rdpersoncamera.h>

ce3rdPersonCamera::ce3rdPersonCamera (const iViewport* viewport)
	: ceCamera (viewport)
	, distance (10.0f)
	, angleH (0.0f)
	, angleV (0.0f)
{
	D3DXMatrixIdentity (&matrix);
}

void ce3rdPersonCamera::SetAngleH (float angleH)
{
	this->angleH = angleH;
}

float ce3rdPersonCamera::GetAngleH () const
{
	return angleH;
}

void ce3rdPersonCamera::SetAngleV (float angleV)
{
	this->angleV = angleV;
}

float ce3rdPersonCamera::GetAngleV () const
{
	return angleV;
}

void ce3rdPersonCamera::SetDistance (float distance)
{
	this->distance = distance;
}

float ce3rdPersonCamera::GetDistance () const
{
	return distance;
}

void ce3rdPersonCamera::Update ()
{
	D3DXMATRIX matH, matV;

	// create  the rotation matrices... and combine them
	D3DXMatrixRotationX (&matH, angleH);
	D3DXMatrixRotationY (&matV, angleV);
	D3DXMatrixMultiply (&matrix, &matH, &matV);

	D3DXVECTOR4 u (0, 1, 0, 0);
	D3DXVECTOR4 p (0, 0, distance, 0);
	D3DXVec4Transform (&p, &p, &matrix);
	D3DXVec4Transform (&u, &u, &matrix);

	D3DXVECTOR3 f (p.x, p.y, p.z);
	D3DXVec3Add (&f, &f, &GetAt());

	SetUp (D3DXVECTOR3 (u.x, u.y, u.z));
	SetFrom (f);

	ceCamera::Update ();
}

void ce3rdPersonCamera::RotateH (float angle)
{
	angleH += angle;
}

void ce3rdPersonCamera::RotateV (float angle)
{
	angleV += angle;
}

void ce3rdPersonCamera::Move (const D3DXVECTOR3& dir)
{
	static D3DXVECTOR4 v4;
	D3DXVec3Transform (&v4, &dir, &matrix);
	MoveAt ((const D3DXVECTOR3&)v4);
}


void ce3rdPersonCamera::Move (float x, float y, float z)
{
	static D3DXVECTOR4 v4;

	v4.x = x;
	v4.y = y;
	v4.z = z;
	v4.w = 0.0f;

	D3DXVec4Transform(&v4, &v4, &matrix);

	MoveAt ((const D3DXVECTOR3&)v4);
}
